package com.itheima.demo6;

import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo1 {
    public static void main(String[] args) throws IOException {

        //1.定义变量记录网址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";

        String snn = webCrawler(familyNameNet);
        String bnn = webCrawler(boyNameNet);
        String gnn = webCrawler(girlNameNet);

        ArrayList<String> familyTempList = geData(snn,"(.{4})(，|。)",1);
            ArrayList<String> boyTempList = geData(bnn,"([\\u4E00-\\u9FA5]{2})(、|。)",1);
        ArrayList<String> girlTempList = geData(gnn,"(.. ){4}..",0);

        //处理数据
        ArrayList<String> familyList = new ArrayList<>();
        ArrayList<String> boyList = new ArrayList<>();
        ArrayList<String> girlList = new ArrayList<>();

        //姓的遍历
        for (String s : familyTempList) {
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                familyList.add(c + "");
            }
        }

        //男生名字的遍历: 去重
        for (String s : boyTempList) {
            if (!boyList.contains(s)){
                boyList.add(s);
            }
        }

        //女生的名字的遍历：切割
        for (String s : girlTempList) {
            String[] s1 = s.split(" ");
            for (int i = 0; i < s1.length; i++) {
                girlList.add(s1[i]);
            }
        }

        System.out.println(familyList);
        System.out.println(boyList);
        System.out.println(girlList);

    }

    /**
     * 从网络上爬取数据，把数据拼接成字符串返回
     *
     */
    public static String webCrawler(String net) throws IOException {
        //1.定义StringBuilder拼接爬取到的数据
        StringBuilder sb = new StringBuilder();
        //创建一个URl对象
        URL url = new URL(net);
        //连接网址
        URLConnection conn = url.openConnection();
        //读取数据
        InputStreamReader isr = new InputStreamReader(conn.getInputStream());
        int ch;
        while ((ch = isr.read()) != -1){
            sb.append((char)ch);
        }
        isr.close();
        return sb.toString();

    }

    //根据正则表达式获取字符串中的数据

    private static ArrayList<String> geData(String str,String regex,int index){
        //1.创建集合存放数据
        ArrayList<String> list = new ArrayList<>();
        //2.根据正则表达式的规则，获取数据
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {
            list.add(matcher.group(index));
        }

        return list;
    }

}
